package com.woniuxy.educationplatform.mapper;

import com.woniuxy.educationplatform.bean.ProjectBean;
import com.woniuxy.educationplatform.bean.SubjectBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

@Mapper
public interface SubjectMapper {

    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "subjectName", column = "subject_name", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "projectBean", column = "fk_project_id", javaType = ProjectBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.ProjectMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("select * from subject where id = #{id}")
    SubjectBean findById(Long id);


    /**
     * 查询所有项目
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "subjectName", column = "subject_name", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "projectBean", column = "fk_project_id", javaType = ProjectBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.ProjectMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("select * from subject")
    List<SubjectBean> findAll();

    /**
     * 根据条件查询
     * 这里用的是xml配置文件进行的配置
     * @param id fk_project_id 。
     * @return
     */


    List<SubjectBean> findOneByCondition(@Param("fk") Long id, Date createTime);


    void update(@Param("s") SubjectBean subjectBean);

    /**
     * 新建项目的第二句语句，获得第一句的id，作为关联外键。
     * 这里返回值给课程新增，提供id
     * @param subjectBean
     * @param id
     */
    @Insert("insert into subject (subject_name,fk_project_id,create_time) values(#{s.subjectName},#{pid},now())")
    @SelectKey(keyProperty = "id",keyColumn = "id",before = false,resultType = Integer.class,statement = "select last_insert_id()")

    void saveSubjectBean(@Param("s") SubjectBean subjectBean,@Param("pid") Long id);

    @Delete("delete from subject where id = #{pid}")
    void deletdSubjectBean(@Param("pid") Long id);

    @Select("select id from subject where subject_name = #{subjectName}")
    Long findIdByName(String subjectName);

    @Select("select subject_name from subject")
    String[] getSubjectName();
}
